﻿using KPI.Api.Apis;
using KPI.Data;
using KPI.Entity;
using KPI.Entity.Entitys.Dto;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using TYBase;
using WPF_TY_UI;

namespace CssdAssistantClient.Views
{
    public partial class ViewCreateTestQuestion : TYWindow
    {
        ViewCreateQuestionModel vm = new ViewCreateQuestionModel();
        public ViewCreateTestQuestion()
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewCreateQuestion_Loaded;
            vm.ListOption.CollectionChanged += ListOption_CollectionChanged;
        }

        private void ViewCreateQuestion_Loaded(object sender, RoutedEventArgs e)
        {
            vm.SelectedQtType = vm.ListQtType.FirstOrDefault();
        }

        private List<string> listSingle = new List<string>();
        private List<string> listMutil = new List<string>();
        private void ListOption_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (vm.SelectedQtType.Key)
            {
                case (int)(EnumQtType.SingleSelect):
                    listSingle.Clear();
                    foreach (var item in vm.ListOption)
                    {
                        listSingle.Add(item.OptionName);
                    }
                    break;
                case (int)(EnumQtType.MutilSelect):
                    listMutil.Clear();
                    foreach (var item in vm.ListOption)
                    {
                        listMutil.Add(item.OptionName);
                    }
                    break;
                case (int)(EnumQtType.TrueFalse):
                    break;

            }
        }

        private void LSBComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            vm.ListOption.Clear();
            switch (vm.SelectedQtType.Key)
            {
                case (int)(EnumQtType.SingleSelect):
                case (int)(EnumQtType.MutilSelect):
                    vm.ListOption.Add(new OptionModel { OptionName = "" });
                    vm.ListOption.Add(new OptionModel { OptionName = "" });
                    break;
                case (int)(EnumQtType.TrueFalse):
                    vm.ListOption.Add(new OptionModel { OptionName = "是" });
                    vm.ListOption.Add(new OptionModel { OptionName = "否" });
                    break;

            }
        }

        private async void BtnClick_Create(object sender, RoutedEventArgs e)
        {
            CreateQuestionBankDto createDto = new CreateQuestionBankDto()
            {
                title = vm.Title,
                examType = (int)KPIData.ExamType,
                qtType = vm.SelectedQtType.Key,
                creatorCode = KPIData.user.userId,
                createTime = DateTime.Now.AsTick(),
            };
            if (createDto.qtType != (int)EnumQtType.ShortAnswer)
            {
                foreach (var item in vm.ListOption)
                {
                    createDto.listItemInfo.Add(new QuestionItemInfoVO
                    {
                        content = item.OptionName,
                        score = item.Score,
                        orderId = createDto.listItemInfo.Count,
                    });
                }
                createDto.score = createDto.listItemInfo.Sum(x => x.score);
            }
            else
            {
                createDto.score = vm.Score;
            }
            var rData = await Run(() =>
           {
               return QuestionBankApi.CreateQuestionBank(createDto);
           });
            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            MessageBox.Show("新建成功!");
        }

        private void BtnClick_Cancle(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }



        private void BtnClick_AddOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);

            if (index >= 0)
            {
                vm.ListOption.Insert(index + 1, new OptionModel { });
            }
        }

        private void BtnClick_DeleteOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);
            if (index >= 0)
            {
                vm.ListOption.RemoveAt(index);
            }
        }

        private void BtnClick_UpOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);
            if (index > 0)
            {
                vm.ListOption.RemoveAt(index);
                vm.ListOption.Insert(index - 1, dx);
            }
        }

        private void BtnClick_AddScore(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            dx.Score++;
        }

        private void BtnClick_ReduceScore(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            dx.Score--;
            if (dx.Score < 0)
                dx.Score = 0;
        }

        private void BtnClick_DownOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);
            if (index + 1 < vm.ListOption.Count)
            {
                vm.ListOption.RemoveAt(index);
                vm.ListOption.Insert(index + 1, dx);
            }
        }

        private void BtnClick_AddScore_ShortAnswer(object sender, RoutedEventArgs e)
        {
            vm.Score++;
        }

        private void BtnClick_ReduceScore_ShortAnswer(object sender, RoutedEventArgs e)
        {
            vm.Score--;
            if (vm.Score < 0)
                vm.Score = 0;
        }
    }
}
